﻿#include "ThreeDCloudMapMarkerSettingDialog.h"
#include "ui_ThreeDCloudMapMarkerSettingDialog.h"
#include "Graph3DWindowBase.h"
#include "IdaWidgetHelper.h"
#include "BaseWidget/IdaDialogMsg.h"

namespace pst
{
    ThreeDCloudMapMarkerSettingDialog::ThreeDCloudMapMarkerSettingDialog(QWidget* parent)
        : IdaDialogFrame(parent)
        , ui(new Ui::ThreeDCloudMapMarkerSettingDialog())
    {
        ui->setupUi(this);
        setWindowTitle("显示设置");
        setWindowSize(322, 185);
        for (int i = 0; i < 50; i++)
        {
            ui->comboBox_markerActorSize->addItem(QString::number(i));
            ui->comboBox_fontSize->addItem(QString::number(i));
        }
        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_confirm); // 将确定设置为主按钮
    }

    ThreeDCloudMapMarkerSettingDialog::~ThreeDCloudMapMarkerSettingDialog()
    {
        delete ui;
    }

    void ThreeDCloudMapMarkerSettingDialog::updateMarker3DDataInfo(Marker3DDataInfo* marker3DDataInfo)
    {
        if (marker3DDataInfo == nullptr)
        {
            return;
        }
        m_marker3DDataInfo = marker3DDataInfo;
        ui->lineEdit_markName->setText(marker3DDataInfo->m_markerName);
        ui->comboBox_markerActorSize->setCurrentText(QString::number(marker3DDataInfo->m_markerSize));
        ui->comboBox_fontSize->setCurrentText(QString::number(marker3DDataInfo->m_markerTextFontSize));
    }

    void ThreeDCloudMapMarkerSettingDialog::on_pushButton_cancel_clicked()
    {
        this->hideWindow();
    }

    void ThreeDCloudMapMarkerSettingDialog::updateUsingDataFromUI()
    {

        QString newName = ui->lineEdit_markName->text();

        for (auto it = m_existMarkerMap.cbegin(); it != m_existMarkerMap.cend(); ++it)
        {
            int index = it.key();
            if (index != m_currentMarkerIndex)
            {
                if (newName == m_existMarkerMap.value(index))
                {
                    m_execSuccessFlag = false;
                    IdaDialogMsg::showDialog(this, QString("名称已存在，请重新填写。"), EDialogMsg::Error, tr("重命名错误"));
                    return;
                }
            }
        }

        m_marker3DDataInfo->m_markerName = ui->lineEdit_markName->text();
        m_marker3DDataInfo->m_markerSize = ui->comboBox_markerActorSize->currentText().toInt();
        m_marker3DDataInfo->m_markerTextFontSize = ui->comboBox_fontSize->currentText().toInt();
    }


    void ThreeDCloudMapMarkerSettingDialog::on_pushButton_apply_clicked()
    {
        m_execSuccessFlag = true;
        updateUsingDataFromUI();
        if (!m_execSuccessFlag)
        {
            return;
        }
        emit signal_marker3DDataInfoChanged(m_marker3DDataInfo);
    }

    void ThreeDCloudMapMarkerSettingDialog::on_pushButton_confirm_clicked()
    {
        m_execSuccessFlag = true;
        updateUsingDataFromUI();
        if (!m_execSuccessFlag)
        {
            return;
        }
        emit signal_marker3DDataInfoChanged(m_marker3DDataInfo);
        this->hideWindow();
    }

    void ThreeDCloudMapMarkerSettingDialog::setCurrentMarkerIndex(int index)
    {
        m_currentMarkerIndex = index;
    }

    void ThreeDCloudMapMarkerSettingDialog::setExistMarkerMap(QHash<int, QString> map)
    {
        m_existMarkerMap = map;
    }
}
